package com.ksr.logistics.controller;

import com.github.pagehelper.PageInfo;
import com.ksr.logistics.pojo.Permission;
import com.ksr.logistics.pojo.Result;
import com.ksr.logistics.pojo.User;
import com.ksr.logistics.service.PermissionService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/permissionData")
    @RequiresPermissions("permission:permissionData")
    @ResponseBody
    public List<Permission> permissionData(){
        return permissionService.selPermissions();
    }

    @RequestMapping("/list")
    @RequiresPermissions("permission:list")
    @ResponseBody
    public PageInfo<Permission> show(Integer pageNum, Integer pageSize, String keyword){
        List<Permission> permissions=permissionService.selPagePermissions(pageNum,pageSize,keyword);
        PageInfo<Permission> infos=new PageInfo<>(permissions);
        if (infos.getSize()==0 && infos.getPageNum()>1){
            permissions=permissionService.selPagePermissions(pageNum-1,pageSize,keyword);
            infos=new PageInfo<>(permissions);
        }
        return infos;
    }

    @RequestMapping("/delete")
    @RequiresPermissions("permission:delete")
    @ResponseBody
    public Result delete(Integer id){
        return permissionService.deleteById(id);
    }

    @RequestMapping("/deletes")
    @RequiresPermissions("permission:deletes")
    @ResponseBody
    public Result deletes(@RequestParam("ids[]") List<Integer> ids){
        return permissionService.deleteByIds(ids);
    }

    @RequestMapping("/edit")
    @RequiresPermissions("permission:edit")
    public String permissionEdit(Model m,Integer permissionId){
        if (null!=permissionId){
            Permission permission=permissionService.selById(permissionId);
            m.addAttribute("permission",permission);
        }
       List<Permission> permissions=permissionService.selPermissions();
       m.addAttribute("permissions",permissions);
       return "permissionEdit";
    }

    @RequestMapping("/insert")
    @RequiresPermissions("permission:insert")
    @ResponseBody
    public Result insert(Permission p){
        Result result=new Result(0,"新增权限失败,请重试!");
        int row=permissionService.insertPermission(p);
        if (1==row){
            result.setCode(1);
            result.setMsg("新增权限成功!");
        }

        return result;
    }

    @RequestMapping("/checkPermissionName")
    @RequiresPermissions("permission:checkPermissionName")
    @ResponseBody
    public boolean checkPermissionName(String name,Integer permissionId){
        boolean res=true;
        if (null!=permissionId){
            Permission p=permissionService.selById(permissionId);
            if (name.equals(p.getName())){
                return true;
            }
        }
        List<Permission> list=permissionService.selByPermissionName(name);
        if (list.size()>0){
            res=false;
        }
        return res;
    }

    @RequestMapping("/update")
    @RequiresPermissions("permission:update")
    @ResponseBody
    public Result update(Permission p){
        Result result=new Result(0,"修改失败,请重试");
        int row=permissionService.updatePermission(p);
        if (1==row){
            result.setCode(1);
            result.setMsg("账号信息修改成功!");
        }

        return result;
    }

    @RequestMapping("/menus")
    @ResponseBody
    public List<Permission> getMenus(){
        Subject subject = SecurityUtils.getSubject();
        User admin = (User) subject.getPrincipal();

        String permissionIds = admin.getPermissionIds();
        if (permissionIds==null){
            permissionIds="-100";
        }
        List<Permission> menus = permissionService.selMenus((long) 0,permissionIds);

//        Long roleId = admin.getRoleId();
//        if (roleId==null){
//            roleId=(long)-100;
//        }
//        List<Permission> menus = permissionService.selMenus((long) 0, roleId);
        return menus;
    }

}
